﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class UserServices : IUserServices
    {
        private readonly UserRepository userRep;

        public UserServices()
        {
            userRep = new UserRepository(new WcfSessionStorage());
        }

        #region IUserServices Members

        public List<UserDTO> GetList(int page, out long recordCount, string sortProperty = null, string sortType = "ASC")
        {
            var Users = new List<User>();
            var userDtos = new List<UserDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Users = userRep.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Users = userRep.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            foreach (User user in Users)
            {
                var userDto = new UserDTO();
                Mapper.Map(user, userDto);
                userDtos.Add(userDto);
            }

            recordCount = (int) userRep.RecordCount;
            return userDtos;
        }

        public void SaveUser(UserDTO userDto)
        {
            User users = userRep.GetDataOne(x => x.UserID == userDto.UserID);
            var user = new User();
            if (users == null)
            {
                users = new User();
                userDto.created_date = DateTime.Now;
                //AutoMapper.Mapper.Map(userDto.created_by, user);
                //userDto.modified_by = null;
            }
            else
            {
                userDto.modified_date = DateTime.Now;
                //AutoMapper.Mapper.Map(userDto.modified_by, user);
            }
            Mapper.Map(userDto, users);
            userRep.SaveOrUpdate(users);
        }

        public void DeleteUser(UserDTO userDto)
        {
            var users = new User();
            Mapper.Map(userDto, users);
            userRep.Delete(users);
        }

        public List<UserDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                               out long recordCount)
        {
            var Results = new List<UserDTO>();
            var users = new List<User>();

            users = userRep.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = userRep.RecordCount;

            users.ForEach(x =>
                              {
                                  var userDto = new UserDTO();
                                  Mapper.Map(x, userDto);
                                  Results.Add(userDto);
                              });
            return Results;
        }


        public bool ValidateUser(UserDTO userDto, out UserDTO result, out string SessionID,out string errorMessage)
        {
            errorMessage = string.Empty;
            SessionID = null;
            result = null;
            bool res = false;
            try
            {
                User user = userRep.GetDataOne(x => x.UserName == userDto.UserName && x.Password == userDto.Password);
                result = new UserDTO();
                Mapper.Map(user, result);

                if (user != null)
                {
                    var dict = new Dictionary<string, UserDTO>();
                    SessionID = result.UserID;
                    dict.Add(SessionID, result);
                    MyContext.Identity = dict;
                    res =  true;
                }
                else
                {
                    SessionID = null;
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return res;
        }

        public bool IsAuthenticated(out UserDTO user, string sessionID)
        {
            bool res = false;
            user = new UserDTO();
            if (!string.IsNullOrEmpty(sessionID))
            {
                res = MyContext.Identity.ContainsKey(sessionID);
                user = MyContext.Identity[sessionID];
            }
            else
            {
                res = false;
            }

            return res;
        }

        #endregion
    }
}